package com.zmn.oms.zmn.business.impl.wechat;

import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.business.interfaces.messageV1.wechat.MessageWeChatTemplateService;
import com.zmn.oms.business.interfaces.warranty.OrderWarrantyBService;
import com.zmn.oms.common.constant.OrderFailConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.constant.WechatQrConststs;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.wechat.WechatQrScanService;
import com.zmn.oms.zmn.business.interfaces.work.orderpay.ZsOrderPayBService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 类描述： 扫码业务
 *
 * @author yule
 * @date 2020/6/23 17:28
 */
@Service
@Slf4j
public class WechatQrScanServiceImpl implements WechatQrScanService {

    private Logger logger = LoggerFactory.getLogger(WechatQrScanServiceImpl.class);
    @Autowired
    OrderWorkService orderWorkService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    OrderWarrantyBService orderWarrantyBService;
    @Autowired
    MessageWeChatTemplateService messageWeChatTemplateService;
    @Autowired
    ZsOrderPayBService zsOrderPayBService;

    @Override
    public void processWechatQrScan(Long orderId, Integer shopId, Long userId, String openId,Integer groupId) {

        /*OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, orderId);
        if (Objects.isNull(orderWork)) {
            log.error("#oms#processWarranty#{} 订单不存在", orderId);
            return;
        }

        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderWork.getOrderId());
        if (orderDetail == null) {
            return;
        }

        // 保修卡扫码
        if (Objects.equals(MessageConsts.ZMN_MESSAGE_WARRANTY_CARD, groupId)) {
            // 绑定
            orderWarrantyBService.takeWarranty(orderId, userId);
            logger.info("【{}】扫码开始发送消息openId【{}】", orderId, openId);
            if (Objects.equals(GlobalConsts.PLAT_MARK_CNHB, orderWork.getPlatWork())) {
                groupId = MessageConsts.CNHB_MESSAGE_WARRANTY_CARD;
            }
            messageWeChatTemplateService.sendDesignatedOpenIdWeChatTemplate()
            boolean isSend = sendOrderNodeNoticeService.sendWechatQrScanMessage(orderWork.getPlatWork(), groupId, openId, userId, orderWork, orderDetail);
            logger.info("扫码发送保修卡信息 状态【{}】", isSend);
        }
        // 定金待支付
        else if (Objects.equals(MessageConsts.ZMN_MESSAGE_DEPOSIT_PAY, groupId)) {
            logger.info("【{}】扫码开始发送定金待支付消息openId【{}】", orderId, openId);
            boolean isSend = sendOrderNodeNoticeService.sendWechatQrScanMessage(orderWork.getPlatWork(), groupId, openId, userId, orderWork, orderDetail);
            logger.info("扫码发送定金待支付 状态【{}】", isSend);
        }
        // 完成待支付
        else if (Objects.equals(MessageConsts.ZMN_MESSAGE_ORDER_PAY, groupId)) {
            logger.info("【{}】扫码开始发送完成待支付消息openId【{}】", orderId, openId);
            boolean isSend = sendOrderNodeNoticeService.sendWechatQrScanMessage(orderWork.getPlatWork(), groupId, openId, userId, orderWork, orderDetail);
            logger.info("扫码发送完成待支付 状态【{}】", isSend);
        }*/

    }

    @Override
    public String getWeChatQrCodeUrl(Long orderId, Integer masterId) throws OmsBaseException {

        OrderWork orderWork = orderWorkService.findMasterWorkDetail(orderId, orderId, masterId);
        if (orderWork == null) {
            throw new OmsBaseException(OrderFailConsts.FAIL_DATA_CHANGE_ORDER_NOMASTER, "无效的工单");
        }

        if (!Objects.equals(orderWork.getResultStatus(), OrderStatusConsts.WORK_RESULT_DOING)) {
            throw new OmsBaseException("订单已取消");
        }

        // 获取二维码
        String qrCode = zsOrderPayBService.getWechatQrcode(orderWork, WechatQrConststs.QR_TYPE_USER_ATTENTION);
        if (StringUtil.isBlank(qrCode)) {
            throw new OmsBaseException("获取微信公众号失败");
        }
        return qrCode;
    }


}